package org.springframework.test.scope;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;

/**
 * [简要描述]:
 * [详细描述]:
 *
 * @author ZouHaiBo
 * @version 1.0, 2021/7/29 19:09
 * @since JDK 1.8
 */
public class CustomScope implements Scope
{

	private ThreadLocal<Object> local = new ThreadLocal<>();

	@Override
	public Object get(String name, ObjectFactory<?> objectFactory)
	{
		if (local.get() != null){
			return local.get();
		}
		Object object = objectFactory.getObject();
		local.set(object);
		return object;
	}

	@Override
	public Object remove(String name)
	{
		Object o = local.get();
		local.remove();
		return o;
	}

	@Override
	public void registerDestructionCallback(String name, Runnable callback)
	{

	}

	@Override
	public Object resolveContextualObject(String key)
	{
		return null;
	}

	@Override
	public String getConversationId()
	{
		return null;
	}
}
